-- zooro_slime
-- created by cheny3
-- 佐罗冈布奥触发事件（触发直接走通用流程）

-- 内部函数声明
local whenMonsterDie;

-- 关注怪物死亡的事件
EventMgr.register("slime_event/dungeon_event", event.MONSTER_DIE_LOGIC, function(args)
    whenMonsterDie(args);
end);

-- 获得怪物死亡的回调
whenMonsterDie = function(args)
    local monster = args.monster;
    if not monster then
        return;
    end

    -- 玩家不在迷宫中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();
    local baseClassId = monster:query("base_class_id");

    -- 佐罗的面罩
    zooroMask(dungeonId, baseClassId);

    -- 佐罗的面罩2
    zooroMask2(dungeonId, baseClassId);
end

-- 佐罗的面罩
zooroMask2 = function(dungeonId, baseClassId)
    local monsterId = FormulaM.invoke("CALC_ZOORO_MONSTER_DEMAND", dungeonId);

    if baseClassId ~= monsterId then
        -- 不是需求怪物
        return;
    end

    -- 判断玩家是否装备佐罗的面罩
    local zooroMaskClassIds = FormulaM.invoke("ZOORO_MASK_CLASS_IDS2");
    local hasZooroMask = false;
    local zooroMaskId = nil;
    local rank = 0;

    for _, classId in pairs(zooroMaskClassIds) do
        if EquipM.isEquiped(classId) then
            hasZooroMask = true;
            zooroMaskId = classId;
            rank = EquipM.getRealRank(classId);
            break;
        end
    end

    if not hasZooroMask then
        -- 没有装备可以升级的佐罗面罩
        return;
    end

    -- 属性加1
    local value = 1;
    -- 成长增加属性
    local prop = PropM.combine(ME.user, "zorro_mask", 1);
    value = PropM.apply(prop, value);

    DungeonServiceM.addDungeonAttrib("zooro_mask_kill2", value);

    local upgradeMaskInfo = FormulaM.invoke("CALC_ZOORO_MASK_UPGRADE2", rank);
    local upgradeMaskNum  = upgradeMaskInfo[1];
    local nextClassId     = upgradeMaskInfo[2];

    local cost = { ["zooro_mask_kill2"] = upgradeMaskNum, };

    if DungeonServiceM.queryDungeonAttrib("zooro_mask_kill2") >= upgradeMaskNum then
        -- 可以升级
        if not DungeonServiceM.costDungeonAttrib(cost) then
            -- 扣除属性失败
            return;
        end

        -- 直接奖励
        BonusM.doBonus({1, nextClassId, 1}, "zooro_mask_upgrade2");

        -- 替换装备/不走地牢ACTION
        EquipM.equip(nextClassId, true, true);

        -- 扣除旧面罩
        ItemM.costAmount(ME.user, zooroMaskId, 1);

        local monsterName = MonsterM.query(baseClassId, "name");
        -- 弹出奖励提示
        EventMgr.fire(event.ZOORO_UPGRADE_TIP,
            {["classId"] = nextClassId, ["monsterName"] = monsterName});
    end
end

-- 佐罗的面罩
zooroMask = function(dungeonId, baseClassId)
    if dungeonId ~= 2 and DungeonAreaM.getParentId(dungeonId) ~= 2 then
        -- 不是英雄的村
        return;
    end

    if baseClassId ~= 24 then
        -- 不是强盗
        return;
    end

    -- 判断玩家是否装备佐罗的面罩
    local zooroMaskClassIds = FormulaM.invoke("ZOORO_MASK_CLASS_IDS");
    local hasZooroMask = false;
    local zooroMaskId = nil;
    local rank = 0;

    for _, classId in pairs(zooroMaskClassIds) do
        if EquipM.isEquiped(classId) then
            hasZooroMask = true;
            zooroMaskId = classId;
            rank = EquipM.getRealRank(classId);
            break;
        end
    end

    if not hasZooroMask then
        -- 没有装备可以升级的佐罗面罩
        return;
    end

    -- 属性加1
    local value = 1;
    -- 成长增加属性
    local prop = PropM.combine(ME.user, "zorro_mask", 1);
    value = PropM.apply(prop, value);

    DungeonServiceM.addDungeonAttrib("zooro_mask_kill", value);

    local upgradeMaskInfo = FormulaM.invoke("CALC_ZOORO_MASK_UPGRADE", rank);
    local upgradeMaskNum  = upgradeMaskInfo[1];
    local nextClassId     = upgradeMaskInfo[2];

    local cost = { ["zooro_mask_kill"] = upgradeMaskNum, };

    if DungeonServiceM.queryDungeonAttrib("zooro_mask_kill") >= upgradeMaskNum then
        -- 可以升级
        if not DungeonServiceM.costDungeonAttrib(cost) then
            -- 扣除属性失败
            return;
        end

        -- 直接奖励
        BonusM.doBonus({1, nextClassId, 1}, "zooro_mask_upgrade");

        -- 替换装备/不走地牢ACTION
        EquipM.equip(nextClassId, true, true);

        -- 扣除旧面罩
        ItemM.costAmount(ME.user, zooroMaskId, 1);

        local monsterName = MonsterM.query(baseClassId, "name");
        -- 弹出奖励提示
        EventMgr.fire(event.ZOORO_UPGRADE_TIP,
            {["classId"] = nextClassId, ["monsterName"] = monsterName});
    end
end

return {
    checkEvent = function(eventId, para)
        -- 迷宫内冈布奥通用判断
        return SlimeEventM.checkEventForDungeonSlime(eventId, para);
    end,
};

